<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<title></title>
<script type="text/javascript">
Ext.onReady(function(){
	var userId = '${__SATURN_AUTORITY_USER__.id}';
	Ext.QuickTips.init();
	var resultWidth = Ext.get("form-ct").getWidth();
    var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
	    sm, 
	   {header: "代理ID", width:resultWidth*0.1,dataIndex: 'agent_id', sortable: true},
       {header: "代理人",width:resultWidth*0.1,dataIndex: 'agent_to', sortable: true},
       {header: "代理方式",width:resultWidth*0.1,dataIndex: 'agent_type', sortable: true,renderer:showType},
       {header: "生效时间",width:resultWidth*0.15,dataIndex: 'start_time', sortable: true},
       {header: "终止时间",width:resultWidth*0.15,dataIndex: 'end_time', sortable: true}
	 ]); 
	cm.defaultSortable = true; 
	var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/agentModule/action/QueryAgentListByUserId.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
		   		   {name: 'agent_id', mapping: 'agent_id'},
	               {name: 'agent_to', mapping: 'agent_to'},
	               {name: 'agent_type', mapping: 'agent_type'},
	               {name: 'start_time', mapping: 'start_time'}, 
	               {name: 'end_time', mapping: 'end_time'}
	               ]
		});
		store.load({params: {start:0,limit:20,userId:userId}});
	    store.on('beforeload',function(){
			 	Ext.apply(this.baseParams,{userId:userId});
       });
		var grid = new Ext.grid.GridPanel({
			    frame:true,
			    bodyStyle:'width:100%',
			    title:'委托出去的代理',
	       	    store: store,
			    sm:sm,
			    cm:cm,
		        renderTo:document.body,
		        height:500,
		        frame:true,
		        loadMask: {msg:'正在加载数据，请稍侯……'},
        		bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
	    		}),
	    		tbar:new Array(
			   new Ext.Toolbar.Button({
						   text:'删除',
						   xtype:"button" ,
						   tooltip:"删除代理关系" ,
						   handler: deleteAgent,
						   scope :this}),
			   new Ext.Toolbar.Button({
						   text:'添加完全代理',
						   xtype:"button" ,
						   tooltip:"添加完全代理" ,
						   handler: addAll,
						   scope :this}),
			  new Ext.Toolbar.Button({
						   text:'添加部分代理',
						   xtype:"button" ,
						   tooltip:"添加部分代理" ,
						    handler: addPart,
						   scope :this})
			   
		 ) 
		});
		
    	//完全代理	
       function addAll(){	
       		window.location="allAgent.jsp";
       }
       //部分代理	
       function addPart(){	
       		window.location="partAgent.jsp";
       }
       function deleteAgent(){
	    var agentIds = sm.getSelections();
	 	if(agentIds.length==0){
	 		Ext.MessageBox.alert('警告','至少选择一条记录');
	 	}else{
	 		Ext.MessageBox.confirm('确认','确定删除所选择的数据?',delAgent);
	 	}
	 }
	  //删除代理
	 function delAgent(btn){
	 	if(btn == 'yes'){
	        arr_agentid = sm.getSelections();
	        var delStr = ''; 		 
	   	   	for (var i = 0, len = arr_agentid.length; i < len; i++){    	   	   	  	 
	   	   	  	 delStr += arr_agentid[i].data["agent_id"];  
	   	   	  	 delStr += ',' ;	
			} 
		    if(delStr.length > 0){
 					delStr = delStr.substring(0,delStr.length - 1) ;
			}
			Ext.Ajax.request({
			     method : 'POST',                           
			     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.client::/agentModule/action/DeleteAgent.action',     
			     success:function(request){    
			       store.load({params:{start:0, limit:20}});                
			       var myResult = Ext.util.JSON.decode(request.responseText);
			       Ext.Msg.alert('信息',myResult.message);        
			     },
			     failure:function(){                          
			       Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
			     },
			     params:{
			     		agentIds:delStr 
			     } 
	     	});
       	}
	 }
	 
	 function showType(value){
	 	if(value=='PART'){
	 		return '部分代理';
	 	}
	 	if(value=='ALL'){
	 		return '完全代理';
	 	}
	 }
});
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>